package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysDoctorSchedule;
import com.ruoyi.system.domain.SysDoctorScheduleQuery;
import com.ruoyi.system.domain.DoctorSchedules;
import com.ruoyi.system.service.ISysDoctorScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/doctorSchedule")
public class SysDoctorScheduleController extends BaseController {
    @Autowired
    private ISysDoctorScheduleService scheduleService;

    @PreAuthorize("@ss.hasPermi('system:doctorSchedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDoctorScheduleQuery query) {
        startPage();
        List<DoctorSchedules> list = scheduleService.selectScheduleList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:doctorSchedule:query')")
    @GetMapping("/{doctorId}/{date}")
    public AjaxResult detail(@PathVariable Long doctorId, @PathVariable String date) {
        SysDoctorSchedule detail = scheduleService.selectScheduleDetail(doctorId, DateUtils.parseDate(date));
        return AjaxResult.success(detail);
    }

    @PreAuthorize("@ss.hasPermi('system:doctorSchedule:edit')")
    @PostMapping("/set")
    public AjaxResult set(@RequestBody SysDoctorSchedule schedule) {
        return toAjax(scheduleService.insertOrUpdateSchedule(schedule));
    }

    @PreAuthorize("@ss.hasPermi('system:doctorSchedule:edit')")
    @PostMapping("/close")
    public AjaxResult close(@RequestBody Map<String, Object> params) {
        Long doctorId = Long.valueOf(params.get("doctorId").toString());
        String date = params.get("scheduleDate").toString();
        String period = params.get("period").toString();
        return toAjax(scheduleService.closeSchedule(doctorId, DateUtils.parseDate(date), period));
    }
    @PostMapping
    public AjaxResult add(@RequestBody SysDoctorSchedule schedule) {
        int rows = scheduleService.insertSysDoctorSchedule(schedule);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error("新增失败");
    }
}